//
//  HuiTool.m
//  huikandian
//
//  Created by ztfgz on 2018/9/18.
//  Copyright © 2018年 ztfgz. All rights reserved.
//

#import "HuiTool.h"

@implementation HuiTool

/**
 *  16进制颜色值转RGB
 *
 *  @param hexString 16进制字符串色值
 *
 *  @return RGB色值
 */
+ (UIColor *)colorWithHexString:(NSString *)hexString
{
    NSString *cString = [[hexString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    
    if ([cString length] < 6)
        return [UIColor clearColor];;
    unsigned int alpha = 255;
    if ([cString hasPrefix:@"0X"])
        cString = [cString substringFromIndex:2];
    if ([cString hasPrefix:@"#"])
        cString = [cString substringFromIndex:1];
    NSRange range = NSMakeRange(0, 2);
    if ([cString length]==8) {
        [[NSScanner scannerWithString:[cString substringWithRange:range]] scanHexInt:&alpha];
        cString = [cString substringFromIndex:2];
    }
    if ([cString length] != 6)
        return [UIColor clearColor];
    
    
    
    unsigned int red, green, blue;
    [[NSScanner scannerWithString:[cString substringWithRange:range]] scanHexInt:&red];
    
    range.location = 2;
    [[NSScanner scannerWithString:[cString substringWithRange:range]] scanHexInt:&green];
    
    range.location = 4;
    [[NSScanner scannerWithString:[cString substringWithRange:range]] scanHexInt:&blue];
    ;
    //    CGColorSpaceRef cmykSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
    //    CGFloat cmykValue[] = {((float)red / 255.0f),((float)green / 255.0f),((float)blue / 255.0f),((float)alpha / 255.0f)};      // blue
    //    CGColorRef colorCMYK = CGColorCreate(cmykSpace, cmykValue);
    //    CGColorSpaceRelease(cmykSpace);
    //    AlmasLog(@"colorCMYK: %@", colorCMYK);
    //
    //    // color with CGColor, uicolor will just retain it
    //    UIColor *color = [UIColor colorWithCGColor:colorCMYK];
    //    return color;//[UIColor colorWithCGColor:CGColorCreate(CGColorSpaceCreateDeviceRGB(), @[((float)red / 255.0f),((float)green / 255.0f),((float)blue / 255.0f),((float)alpha / 255.0f)])];
    //
    
    //    return [DinnerTools convertColor:[UIColor colorWithRed:((float)red / 255.0f)
    //                           green:((float)green / 255.0f)
    //                            blue:((float)blue / 255.0f)
    //                           alpha:((float)alpha / 255.0f)]];
    
    return [UIColor colorWithRed:((float)red / 255.0f)
                           green:((float)green / 255.0f)
                            blue:((float)blue / 255.0f)
                           alpha:((float)alpha / 255.0f)];
}

+ (UIImage *)createImageWithColor:(UIColor *)color
{
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return theImage;
}

+(void)pushViewControllerSrc:(UIViewController*)src to:(UIViewController*)to animated:(BOOL)anim{
    
    /** 限制所有控制器被重复打开 **/
    if ([APP.navigationController.viewControllers[APP.navigationController.viewControllers.count-1] isKindOfClass:[to class]]) {
        return;
    }
    [APP.navigationController pushViewController:to animated:anim];
    
}
+(void)popViewController:(UIViewController*)uvc withAnim:(BOOL)anim{
    
    [APP.navigationController popViewControllerAnimated:anim];
}


@end
